package multiThreading;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class H {
    private static Map<Long, String> map = new HashMap<>();
    //ConcurrentHashMap相对于HashMap的锁分段技术的好处
    public static void main(String arg[]) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (long i = 0; i < 100; i++) {
                    map.put(i, "Fuck");
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    map.put(1000L, "1000");
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (Iterator<Map.Entry<Long, String>> it = map.entrySet().iterator(); it.hasNext(); ) {
                    Map.Entry<Long, String> i = it.next();
                    System.out.println(i.getKey() + ":" + i.getValue());
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }

}
